package com.md2ppt.engine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.ArrayList;

/**
 * 演示文稿引擎工厂
 * 自动选择最合适的可用引擎
 */
public class EngineFactory {
    
    private static final Logger logger = LoggerFactory.getLogger(EngineFactory.class);
    
    private static final List<Class<? extends PresentationEngine>> ENGINE_CLASSES = List.of(
        AsposeEngine.class,  // 优先使用Aspose
        PoiEngine.class      // 回退到POI
    );
    
    /**
     * 获取最佳可用引擎
     */
    public static PresentationEngine getBestAvailableEngine() {
        for (Class<? extends PresentationEngine> engineClass : ENGINE_CLASSES) {
            try {
                PresentationEngine engine = engineClass.getDeclaredConstructor().newInstance();
                if (engine.isAvailable()) {
                    logger.info("选择引擎: {} ({})", 
                               engine.getEngineName(), 
                               getCapabilityDescription(engine.getCapabilities()));
                    return engine;
                }
            } catch (Exception e) {
                logger.debug("引擎 {} 初始化失败: {}", engineClass.getSimpleName(), e.getMessage());
            }
        }
        
        throw new RuntimeException("没有可用的演示文稿引擎");
    }
    
    /**
     * 根据引擎名称获取引擎
     */
    public static PresentationEngine getEngineByName(String engineName) {
        switch (engineName.toUpperCase()) {
            case "ASPOSE":
            case "ASPOSE_JAVA":
                AsposeEngine asposeEngine = new AsposeEngine();
                if (!asposeEngine.isAvailable()) {
                    logger.warn("Aspose引擎不可用，回退到POI引擎");
                    return new PoiEngine();
                }
                return asposeEngine;
            case "POI":
            case "APACHE_POI":
                return new PoiEngine();
            default:
                logger.warn("未知引擎名称: {}，使用默认引擎", engineName);
                return getBestAvailableEngine();
        }
    }
    
    /**
     * 获取所有可用引擎
     */
    public static List<PresentationEngine> getAllAvailableEngines() {
        List<PresentationEngine> availableEngines = new ArrayList<>();
        
        for (Class<? extends PresentationEngine> engineClass : ENGINE_CLASSES) {
            try {
                PresentationEngine engine = engineClass.getDeclaredConstructor().newInstance();
                if (engine.isAvailable()) {
                    availableEngines.add(engine);
                }
            } catch (Exception e) {
                logger.debug("引擎 {} 不可用: {}", engineClass.getSimpleName(), e.getMessage());
            }
        }
        
        return availableEngines;
    }
    
    /**
     * 检查是否有高级动画支持
     */
    public static boolean hasAdvancedAnimationSupport() {
        try {
            AsposeEngine asposeEngine = new AsposeEngine();
            return asposeEngine.isAvailable() && asposeEngine.getCapabilities().supportsAnimations();
        } catch (Exception e) {
            return false;
        }
    }
    
    /**
     * 获取能力描述
     */
    private static String getCapabilityDescription(PresentationEngine.EngineCapabilities capabilities) {
        List<String> features = new ArrayList<>();
        
        if (capabilities.supportsAnimations()) {
            features.add("动画");
        }
        if (capabilities.supportsAdvancedEffects()) {
            features.add("高级效果");
        }
        if (capabilities.supportsVideoEmbedding()) {
            features.add("视频嵌入");
        }
        if (capabilities.supportsCustomThemes()) {
            features.add("自定义主题");
        }
        
        if (features.isEmpty()) {
            return "基础功能";
        }
        
        return String.join(", ", features);
    }
    
    /**
     * 显示引擎状态报告
     */
    public static void printEngineStatus() {
        logger.info("=== 引擎状态报告 ===");
        
        List<PresentationEngine> engines = getAllAvailableEngines();
        
        if (engines.isEmpty()) {
            logger.error("没有可用的演示文稿引擎！");
            return;
        }
        
        for (PresentationEngine engine : engines) {
            PresentationEngine.EngineCapabilities caps = engine.getCapabilities();
            logger.info("引擎: {} - {}", 
                       engine.getEngineName(), 
                       getCapabilityDescription(caps));
            
            if (caps.supportsAnimations()) {
                logger.info("  最大动画效果数: {}", caps.maxAnimationEffects());
            }
        }
        
        logger.info("推荐引擎: {}", getBestAvailableEngine().getEngineName());
    }
}